package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.dto.LabListDto;
import com.zito.health.dto.LaboratoryLinkmanDto;
import com.zito.health.entity.Laboratory;
import com.zito.health.entity.LaboratoryCredentials;
import com.zito.health.entity.LaboratoryLinkman;
import com.zito.health.entity.User;
import com.zito.health.service.LabListService;
import com.zito.health.service.LaboratoryLinkmanService;
import com.zito.health.service.LaboratoryService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.LaboratoryCredentialsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * @description: 实验室列表（管理）
 * @author: Sam
 * @time: 2020/03/01 15:57
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/labList")
public class LabListController {

    @Autowired
    private LaboratoryService laboratoryService;
    @Autowired
    private LaboratoryLinkmanService laboratoryLinkmanService;
    @Autowired
    private LabListService labListService;

    /**
     * 实验室备案申报
     * @param laboratory
     * @return
     */
    @PostMapping
    public VerifyResult save(Laboratory laboratory){
        VerifyResult verifyResult = new VerifyResult();
        if(laboratory.getName()==null){
            verifyResult.setResult(false);
            verifyResult.setMsg("实验室名称或申报时间为空");
            return verifyResult;
        }
        User user = CacheUtil.getCacheUser().getUser();
        laboratory.setUserid(user.getUserid());
        laboratory.setUnitId(user.getUnitId());
        laboratory.setStates(8);
        Date date=new Date();
        laboratory.setEstablishTime(date);
        laboratory.setDeclareTime(date);//实验室申报时间
        laboratory.setLife(5);//这个地方得改
        laboratoryService.save(laboratory);
        verifyResult.setMsg(String.valueOf(laboratory.getId()));
        return verifyResult;
    }

    /**
     * 实验室基础信息的保存
     * @param dto
     * @return
     */
    @PostMapping("/saveBase")
    public VerifyResult saveBase(LaboratoryLinkmanDto dto){
        VerifyResult verifyResult = new VerifyResult();
        Laboratory laboratory=new Laboratory();
        LaboratoryLinkman laboratoryLinkman= new LaboratoryLinkman();
        BeanUtils.copyProperties(dto,laboratory);
        BeanUtils.copyProperties(dto,laboratoryLinkman);

        laboratoryService.update(laboratory);

        laboratoryLinkman.setLaboratoryId(dto.getId());
        laboratoryLinkman.setName(dto.getLinkmanName());
        LaboratoryLinkman linkman=new LaboratoryLinkman();
        linkman.setLaboratoryId(dto.getId());

        linkman= laboratoryLinkmanService.queryOne(linkman);
        if(linkman==null){
            laboratoryLinkman.setId(null);
            laboratoryLinkmanService.save(laboratoryLinkman);
        }else{

            laboratoryLinkman.setId(linkman.getId());
            laboratoryLinkmanService.update(laboratoryLinkman);
        }
        return verifyResult;
    }

    /**
     * 实验室基础信息
     * @param laboratory
     * @return
     */
    @PutMapping
    public VerifyResult update(Laboratory laboratory){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryService.update(laboratory);
        return verifyResult;
    }

    @GetMapping
    public PageResult list(LabListDto labListDto, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        System.out.println("测试");
        PageHelper.startPage(pageNo,pageSize);
        List<LabListDto> list = labListService.queryLabList(labListDto);
        PageInfo<LabListDto> pageInfo = new PageInfo(list);
        PageResult<LabListDto> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @GetMapping("/{id}")
    public Laboratory queryById(@PathVariable Integer id){
        Laboratory laboratory = laboratoryService.queryById(id);
        return laboratory;
    }

    @GetMapping("/queryCertificateByLabId/{laboratoryId}")
    public LaboratoryCredentialsVo queryCertificateByLabId(@PathVariable Integer laboratoryId){
        System.out.println("测试查询");
        LaboratoryCredentialsVo laboratoryCredentialsVo = labListService.queryCertificateByLabId(laboratoryId);
        return laboratoryCredentialsVo;
    }


    /**
     * 实验室删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public VerifyResult delete(@PathVariable Integer id){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryService.deleteById(id);
        return verifyResult;
    }


}
